package com.example.demo.security;

import com.example.demo.security.bean.SecurityResourceBean;
import com.example.demo.security.bean.SecurityRoleBean;
import com.example.demo.security.bean.SecurityRoleResourceBean;
import com.example.demo.security.service.SecurityResourceServer;
import com.example.demo.security.service.SecurityRoleResourceService;
import com.example.demo.security.service.SecurityRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.jvm.hotspot.debugger.Page;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/Admin/role")
public class RoleController {

    @Value("${config.page.size}")
    private int pageSize;


    @Autowired
    SecurityRoleService roleService;

    @Autowired
    SecurityResourceServer resourceServer;

    @Autowired
    SecurityRoleResourceService roleResourceService;

    String rootName = "/security/";

    @RequestMapping("rolelist")
    public String RoleList(@RequestParam(name = "page",required = false) String page, Model model){
        PageRequest pageRequest = null;
        if (null==page){
            pageRequest = new PageRequest(0,pageSize);
        }else{
            pageRequest= new PageRequest(Integer.valueOf(page)-1,pageSize);
        }
        model.addAttribute("rolelist",roleService.getAll(pageRequest));
        return rootName+"rolelist";
    }


    @RequestMapping("addrole")
    public String addRole(Model model){
        model.addAttribute("resourcelist",resourceServer.getAll());
        model.addAttribute("entity",new SecurityRoleBean());
        model.addAttribute("selectresource",null);
        return rootName+"addrole";
    }

    @RequestMapping("editrole/{id}")
    public String getRole(@PathVariable int id, Model model){
        model.addAttribute("resourcelist",resourceServer.getAll());
        SecurityRoleBean bean = roleService.getById(id);
        model.addAttribute("entity",bean);

        System.out.println(roleResourceService.getResource(bean).size());
        model.addAttribute("selectresource",roleResourceService.getResource(bean));
        return rootName+"addrole";
    }

    @RequestMapping("delrole/{id}")
    public String delRole(){
        return "redirect:/Admin/role/rolelist";
    }

    @RequestMapping("saverole")
    public String saveRole(@Valid SecurityRoleBean bean, BindingResult result,String[] roleResourceBeanSet){
        roleService.save(bean,roleResourceBeanSet);
        System.out.println(bean.getNickName());
       return "redirect:/Admin/role/rolelist";
    }

}
